'''This basically runs through files looking for erroneous words.'''
############################################################################
#    Copyright (C) 2006 by Robert Small                                    #
#    SmallR2002@Gmail.com                                                  #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################
import sys
import enchant
import string #IGNORE:W0402
import os
import collections
import operator

sys.path.append(os.path.expanduser("~") + "/.xchat2/bottlecap")
import bcmisc

words = []
pairs = []
olw = []

d = enchant.Dict(enchant.Dict().tag)

#import previous words
try: 
    for l in open(os.path.expanduser("~") + os.sep + "tmp" + os.sep + "output.log").read().split("\n"):
        olw.append((l.split(": ")[0][1:-1]).lower())
    
    for l in open(os.path.expanduser("~") + os.sep + "tmp" + os.sep + "output_ig.log").read().split("\n"):
        olw.append(l.lower())
finally:
    pass

def ask_question(question):
    ans = raw_input(question + ">")
    if ans == "" or ans == "y" or ans == "yes":
        return True
    elif ans == "n" or ans == "no":
        return False
    else:
        return None

def check_word(word):
    if word.startswith("http"):
        return True

def test_word(word):
    '''
    Checks a word against various dictionaries.
    @param word: the word to check.
    @type word: string
    '''
    if (len(word) > 0)  and (word[0] != '<') and (len([y for y in string.letters if word.count(y)]) > 0 ) and (d.check(word) == False) and (bcmisc.aol.keys().count(word) == 0) and olw.count(word) == 0 and string.punctuation.find(word[:1]) == -1 and string.punctuation.find(word[-1:]) == -1:
        return True
    return False

def scan_file(sfile):
    '''
    Scan file and grab bad words.
    @param sfile: the file to scan.
    @type sfile: string
    '''
    c = []
    output_words = []
    
    lines = [x for x in (open(sfile).read()).split('\n') if (len(x) > 16)  and (x[16] == '<')] 
    
    #contents = set([bcmisc.clean_text(x, True, False, True) for x in set((string.join(lines, ' ')).split())])
    contents = string.join(lines, ' ').split()
    del lines
    
    contents = [x.lower() for x in contents if test_word(x.lower())]
    
    #for c in contents:
    #    output_words.append(c)
    
    return contents
    
for f in sys.argv[1:]:
    words = words + scan_file(f)

counts = collections.defaultdict(int)
for w in words:
    counts[w] += 1
results = counts.items()
results.sort(key=operator.itemgetter(1), reverse=True)

print "Got " + str(len(words)) + " words in total, sorted them to remove duplicates and ordered them by duplicity and concluded with "+ str(len(results)) + " words."

words = results

print "Got " + str(len(words)) + " new words!"

if ask_question("Do you want to create pairs?") == True:
    if ask_question("What about writing to a file?") == True:
        f = open(os.path.expanduser("~") + os.sep + "tmp" + os.sep + "output.log", "a")
        ig = open(os.path.expanduser("~") + os.sep + "tmp" + os.sep + "output_ig.log", "a")
    for w in words:
        if check_word(w[0]):
            pass
        else:
            ans = ask_question("Do you want to pair '" + w[0] + "'?")
            if ans == True:
                ans = raw_input(">")
                pairs.append('"' + w[0] + '": "' + ans + '",')
                if f != None:
                    f.write('"' + w[0] + '": "' + ans + '",\n')
            elif ans == False:
                if ig != None:
                    ig.write(w[0] + "\n")    
    for p in pairs:
        print p
elif ask_question("OK, do you want to output the badwords?") == True:
    for w in words:
            print w